
plot_dat <-
  main_df %>%
  filter(pub_official_indicator == 1) %>%
  group_by(group.treatment) %>%
  summarise(avg_vign = mean(avg_vign, na.rm = T),
            any_vign = mean(any_vign, na.rm = T),
            all_vign = mean(all_vign, na.rm = T)) %>%
  pivot_longer(cols = avg_vign:all_vign, names_to = "cols", values_to = "values")

se1 <- Rmisc::summarySE(main_df %>% filter(pub_official_indicator == 1), measurevar= "avg_vign", groupvars=c("group.treatment"))%>% select(group.treatment, se) %>% mutate(cols = "avg_vign")
se2 <- Rmisc::summarySE(main_df %>% filter(pub_official_indicator == 1), measurevar= "any_vign", groupvars=c("group.treatment")) %>% select(group.treatment, se) %>% mutate(cols = "any_vign")
se3 <- Rmisc::summarySE(main_df %>% filter(pub_official_indicator == 1), measurevar= "all_vign", groupvars=c("group.treatment")) %>% select(group.treatment, se) %>% mutate(cols = "all_vign")

plot_dat <-
  plot_dat %>%
  left_join(., bind_rows(se1, se2, se3))

avg_vign <-
  plot_dat %>%
  filter(cols == "avg_vign") %>%
  ggplot(aes(x=group.treatment, y = values, fill = group.treatment)) +
  geom_bar(stat = "identity", color = "black") +
  geom_errorbar(aes(x=group.treatment, ymin = values - 1.67*se, ymax = values + 1.67*se), width = 0.075) +
  geom_errorbar(aes(x=group.treatment, ymin = values - 1.96*se, ymax = values + 1.96*se), width = 0.05) +
  xlim(c("patronage", "random", "merit")) +
  
  #facet_wrap(cols~., scales = "free_y") +
  ylab("Average Weberianness Score (0-5)") +
  theme_bw()+ 
  theme(axis.title.x = element_blank(),
        axis.line.x.bottom = element_line(color = "black"),
        axis.line.y.left = element_line(color = "black"),
        text = element_text(size=14),
        panel.grid.major = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        axis.ticks = element_blank()) +
  scale_fill_grey() +
  geom_segment(aes(x = 1.1, xend = 1.9, yend = 3.7, y = 3.7))+
  geom_segment(aes(x = 1.9, xend = 2.0, yend = 3.6, y = 3.7))+
  geom_segment(aes(x = 1.1, xend = 1.0, yend = 3.6, y = 3.7))+
  geom_segment(aes(x = 2.1, xend = 2.9, yend = 3.7, y = 3.7))+
  geom_segment(aes(x = 2.9, xend = 3.0, yend = 3.6, y = 3.7)) +
  geom_segment(aes(x = 2.1, xend = 2, yend = 3.6, y = 3.7)) +
  annotate("text", x=1.5, y=3.9, label="+0.39 (p=0.035)", size=4.5) +
  annotate("text", x=2.5, y=3.9, label="+0.04 (p=0.809)", size=4.5) +
  geom_segment(aes(x = 1.1, xend = 2.9, yend = 4.2, y = 4.2))+
  geom_segment(aes(x = 2.9, xend = 3.0, yend = 4.1, y = 4.2)) +
  geom_segment(aes(x = 1.1, xend = 1, yend = 4.1, y = 4.2)) +
  annotate("text", x=2, y=4.4, label="+0.43 (p=0.016)", size=4.5)

avg_vign

all_vign <-
  plot_dat %>%
  filter(cols == "all_vign") %>%
  ggplot(aes(x=group.treatment, y = values*100, fill = group.treatment)) +
  geom_bar(stat = "identity", color = "black") +
  geom_errorbar(aes(x=group.treatment, ymin = values*100 - 1.67*se*100, ymax = values*100 + 1.67*se*100), width = 0.075) +
  geom_errorbar(aes(x=group.treatment, ymin = values*100 - 1.96*se*100, ymax = values*100 + 1.96*se*100), width = 0.05) +
  xlim(c("patronage", "random", "merit")) +
  #facet_wrap(cols~., scales = "free_y") +
  ylab("All Weberian Answers (%)") +
  theme_bw()+ 
  theme(axis.title.x = element_blank(),
        axis.line.x.bottom = element_line(color = "black"),
        axis.line.y.left = element_line(color = "black"),
        text = element_text(size=14),
        panel.grid.major = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        axis.ticks = element_blank()) +
  scale_fill_grey() +
  geom_segment(aes(x = 1.1, xend = 1.9, yend = 41, y = 41))+
  geom_segment(aes(x = 1.9, xend = 2.0, yend = 40, y = 41))+
  geom_segment(aes(x = 1.1, xend = 1.0, yend = 40, y = 41))+
  geom_segment(aes(x = 2.1, xend = 2.9, yend = 41, y = 41))+
  geom_segment(aes(x = 2.9, xend = 3.0, yend = 40, y = 41)) +
  geom_segment(aes(x = 2.1, xend = 2, yend = 40, y = 41)) +
  annotate("text", x=1.5, y=43, label="+14.8pp (p=0.037)", size=4.5) +
  annotate("text", x=2.5, y=43, label="-0.1pp (p=0.99)", size=4.5) +
  geom_segment(aes(x = 1.1, xend = 2.9, yend = 48, y = 48))+
  geom_segment(aes(x = 2.9, xend = 3.0, yend = 47, y = 48)) +
  geom_segment(aes(x = 1.1, xend = 1, yend = 47, y = 48)) +
  annotate("text", x=2, y=50, label="+14.8pp (p=0.048)", size=4.5)



ggsave(filename = "./outputs/figures/figure_3a.pdf", plot = avg_vign, width = 7, height = 5)
ggsave(filename = "./outputs/figures/figure_3b.pdf", plot = all_vign, width = 7, height = 5)
